package com.ruoyi.project.cms.sm.depart.controller;

import com.ruoyi.common.constant.CMSConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.sm.depart.domain.Depart;
import com.ruoyi.project.cms.sm.depart.service.IDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：MrL
 * 时间：2020-05-17
 */
@Controller
@RequestMapping("/cms/sm/depart")
public class DepartController extends BaseController {

    // 当前控制类的请求地址前缀
    private String prefix = "cms/sm/depart";

    @Autowired
    private IDepartService departService;

    /**
     * 转发到 depart.html 页面
     * @return
     */
    @GetMapping()
    public String depart() {
        return prefix + "/depart";
    }

    /**
     * 根据条件分页查询订部门数据
     * @param depart
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Depart depart) {
        startPage();
        List<Depart> list = departService.selectDepartList(depart);
        return getDataTable(list);
    }

    /**
     * 新增部门
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存部门
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Depart depart) {
        if (CMSConstants.DEPART_NAME_NOT_UNIQUE.equals(departService.checkDepartNameUnique(depart))) {
            return error("新增部门'" + depart.getDeptName() + "'失败，部门名称已存在");
        }
        return toAjax(departService.insertDepart(depart));
    }

    /**
     * 批量删除部门
     *
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(departService.deleteDepartByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改部门信息
     */
    @GetMapping("/edit/{deptId}")
    public String edit(@PathVariable("deptId") Long deptId, ModelMap mmap) {
        // 根据类别ID查询订单类别信息
        mmap.put("depart", departService.selectDepartById(deptId));
        return prefix + "/edit";
    }

    /**
     * 修改保存部门信息
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Depart depart) {
        if (CMSConstants.DEPART_NAME_NOT_UNIQUE.equals(departService.checkDepartNameUnique(depart))) {
            return error("修改部门'" + depart.getDeptName() + "'失败，部门名称已存在");
        }
        return toAjax(departService.updateDepart(depart));
    }

    /**
     * 查看部门详情
     *
     * @param deptId
     * @param mmap
     * @return
     */
    @GetMapping("/detail/{deptId}")
    public String detail(@PathVariable("deptId") Long deptId, ModelMap mmap) {
        mmap.put("name", "depart");
        mmap.put("depart", departService.selectDepartById(deptId));
        return prefix + "/detail";
    }

    /**
     * 校验部门名称是否唯一
     */
    @PostMapping("/checkDepartNameUnique")
    @ResponseBody
    public String checkDepartNameUnique(Depart depart) {
        return departService.checkDepartNameUnique(depart);
    }

}
